<?php

namespace app\common\model\haipi;

use think\facade\Db;
use think\Model;

/**
 * Forum
 */
class Forum extends Model
{
    // 表名
    protected $name = 'haipi_forum';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';

    const STATUS_NORMAL = 1;
    const STATUS_DISABLE = 0;
    // 追加属性
   
    
    public function getAreasAttr($value, $data): array
    {
        
       $areas = [$data['province_code'],$data['city_code'],$data['district_code']];

        $value = Db::name('area')->whereIn('code',$areas)->column('id');
      
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setAreasAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getAreasTextAttr($value, $row): string
    {
    
        $row['areas'] = [$row['province_code'], $row['city_code'], $row['district_code']] ??'';

        if ($row['areas'] === '' || $row['areas'] === null) return '';
        $cityNames = Db::name('area')->whereIn('code', $row['areas'])->column('name');
        return $cityNames ? implode(',', $cityNames) : '';
    }

    protected static function onAfterInsert($model): void
    {
        if (is_null($model->weigh)) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }


    
    public function getIsHomeAttr( $value, $row){
        $exist = HomeRecommendation::where('type', HomeRecommendation::TYPE_FORUM)
        ->where('source_id', $row['id'])
        ->field('id')
        ->count() > 0;
        return $exist ? 1 : 0;
    }

    public function getImagesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setImagesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public static  function onAfterWrite($model)
    {
        $data = $model->getData();
        if(!isset($data['is_home'])) {
            return ;
        }
        
        if($data['is_home'] == 1) {
            $exist = HomeRecommendation::where('type', HomeRecommendation::TYPE_FORUM)
            ->where('source_id', $data['id'])
            ->field('id')
            ->count() > 0;
            if(!$exist) {
                HomeRecommendation::create([
                    'type' => HomeRecommendation::TYPE_FORUM,
                    'source_id' => $data['id']
                ]);
            }
        }else{
            HomeRecommendation::where('type', HomeRecommendation::TYPE_FORUM)
                ->where('source_id', $data['id'])
                ->delete();
        }
    }
}